﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;



namespace document_manage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        MySQLConn con = new MySQLConn();
        //扫描路径
        DirectoryInfo theFolder = new DirectoryInfo(@"F:/");

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            Menu w1 = new Menu();
            w1.Show();
            this.Close();
        }


        string res = "";


        //扫描方法
        private  void scan(FileSystemInfo info)
        {
            if (!info.Exists) return;
            DirectoryInfo dir = info as DirectoryInfo;
            try
            {
                FileSystemInfo[] files = dir.GetFileSystemInfos();
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = files[i] as FileInfo;
                    //是文件
                    if (file != null)
                    {
                        ////插入数据库
                        //if (file.Extension == ".docx" || file.Extension == ".txt" || file.Extension == ".xlsx" || file.Extension == ".pdf" || file.Extension == ".pptx")
                        //{
                        //    string sql = "insert into file(file_name,file_path,file_creation_time,file_last_access_time,file_type)" +
                        //      "values('" + file.Name + "','" + file.Directory.ToString().Replace("\\", "\\\\") + "','" + file.CreationTime + "','" + file.LastAccessTime + "','" + file.Extension + "')";
                        //    con.ExecuteQuery(sql);
                        //}
                        res += file.Name + "," + file.Directory.ToString().Replace("\\", "\\\\") + "," + file.CreationTime + "," + file.LastAccessTime + "," + file.Extension + "\r\n";
                    }
                    else scan(files[i]);
                }
            }
            catch (Exception)
            {

                dir = null;
            }
            //不是目录
            if (dir == null) return;
          
        }//end scan

        private void Scan_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Stopwatch w = System.Diagnostics.Stopwatch.StartNew();   //计时器                      
            string sql = "truncate table file ";
            con.ExecuteQuery(sql);
            scan(theFolder);
            w.Stop();
            System.Windows.MessageBox.Show("扫描完成");            
            string s = w.ElapsedMilliseconds.ToString();
            double time = double.Parse(s) / 1000;
            Write(res);

        }

        private void Write(string s)
        {
            string path = @"C:\Users\szj\Desktop\res.txt";

            //用覆盖的方式写入  
            string contents = s;

            File.WriteAllText(path, contents);
            Console.WriteLine("OK");

            Console.ReadKey();
        }

        private void ScanOption_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog m_Dialog = new FolderBrowserDialog();
            DialogResult result = m_Dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            string m_Dir = m_Dialog.SelectedPath.Trim();
            this.Path_TextBox.Text = m_Dir;
            //扫描路径
            theFolder = new DirectoryInfo(@Path_TextBox.Text);
        }
    }
}
